﻿/// <reference path="~/scripts/common.js" />

var LatestTopics = function () {
	var self = this;

	self.Topics = ko.observableArray([]);

	self.Load = function () {
		jQuery.ajax({ url: loadUrl }).done(self.TopicsDataReturned).fail(function (request, error) { console.log(error); });
	};

	self.TopicsDataReturned = function (data) {
		self.Topics.removeAll();
		ko.utils.arrayForEach(data.Topics, function (item) {
			self.Topics.push(new Topic(item.Id, item.Title, item.Posted, item.Author, item.Url));
		});
	};
};

var Topic = function (id, title, posted, author, url) {
	var self = this;

	self.Id = id;
	self.Title = title;
	self.Posted = new Date(posted);
	self.Author = author;
	this.Url = url;

	self.PostedShortString = ko.computed(function () {
		return self.Posted.toLocaleDateString();
	});
};

var model = new LatestTopics();
ko.applyBindings(model, document.getElementById("topics"));

model.Load();
